import xID from './xID.js';

export default class absContainer {
    $root;
    vClickId ='click.'+xID.getID();
    vInnerClick=true;
    /* 定义变量 结束*/
    constructor(el){
        /*
        this.$root = $('#' + el);
        if(this.$root.length==0){
            $(document.body).append(tmplRoot.replace("{{id}}",el));
            this.$root = $('#' + el);
        }
        */
        this.$root = el;
    }

    setOffset(target){
        let wWidth  = window.innerWidth  ;
        let elOffset = $(target).offset() ;
        let cWidth = this.$root.innerWidth();
        let cHeight = $(target).outerHeight();
        let sLeft  = elOffset.left+cWidth+20>wWidth?wWidth-cWidth-30:elOffset.left;
        let sTop  =  elOffset.top+cHeight+4;
        this.$root.css("left",sLeft);
        this.$root.css("top",sTop);
    };
    
    setOffsetLeft(target){
        let targetOffset = $(target).offset() ;
        let rootWidth = this.$root.outerWidth();      
        this.$root.css("left",targetOffset.left-rootWidth-4);
        this.$root.css("top",targetOffset.top);

    };

    setPos($parent,$target){
        let left =0 ;
        let parentOffset = $parent.offset();
        let targetOffset = $target.offset();
        let selfWidth  = this.$root.outerWidth();
        let parentWdith =  $parent.outerWidth();
        //宽度不够
        if(selfWidth > parentWdith-(targetOffset.left-parentOffset.left))
            left = parentWdith - selfWidth-10
        else
            left = targetOffset.left - parentOffset.left+2

        let top = targetOffset.top - parentOffset.top+2;
        this.$root.css("left",left);
        this.$root.css("top",top);
    }

    evOff(e){
        if(this.$root.find(e.target).length>0)
            this.vInnerClick=true;
        if(!this.vInnerClick){
            this.$root.hide();
            $(document).unbind(this.vClickId);
        }
        this.vInnerClick = false;
    }
}